package impl;

import dao.UserDao;
import domain.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户数据的操作实现
 */
public class UserDaoImpl implements UserDao {
    // 新增用户
    public void addUser(User user) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "insert into user (username, password, sex) values (?, ?, ?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, user.getUsername());
            statement.setString(2, user.getPassword());
//            statement.setString(3, user.getTel());
//            statement.setString(4, user.getEmail());
            statement.setString(3, user.getSex());
//            statement.setString(6, user.getPosition());
//            statement.setString(7, user.getRealName());
//            statement.setString(8, user.getUnit());
//            statement.setInt(9, user.getRole().getId());
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, statement, null);
        }

    }

    // 逻辑删除用户
    @Override
    public void deleteUser(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "update user set state = 0 where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, statement, null);
        }
    }

    // 修改用户用户名
    @Override
    public void updateUser(int id, String username) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "update user set username = ? where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, username);
            statement.setInt(2, id);
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, statement, null);
        }
    }

    // 查询用户
    @Override
    public User selectUser(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from user where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
//                String sql2 = "select * from role where id = ?";
//                statement = connection.prepareStatement(sql2);
//                statement.setInt(1, resultSet.getInt("role"));
//                ResultSet resultSet1 = statement.executeQuery();
//                Role role = null;
//                if(resultSet1.next()) {
//                    role = new Role();
//                    role.setId(resultSet1.getInt("id"));
//                    role.setName(resultSet1.getString("name"));
//                    role.setState(resultSet1.getInt("state"));
//                    role.setRemark(resultSet1.getString("remark"));
//                }
//                DBUtil.close(null, null, resultSet1);
                User user = new User();
                user.setId(resultSet.getInt("id"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setTel(resultSet.getString("tel"));
                user.setEmail(resultSet.getString("email"));
                user.setSex(resultSet.getString("sex"));
                user.setPosition(resultSet.getString("position"));
                user.setRealName(resultSet.getString("realname"));
                user.setUnit(resultSet.getString("unit"));
                user.setState(resultSet.getInt("state"));
                // user.setRole(role);
                return user;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }

    // 获取数据库中所有用户信息
    @Override
    public List<User> getUserList() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<User> result = new ArrayList<>();
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from user";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                User user = new User();
                user.setId(resultSet.getInt("id"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setTel(resultSet.getString("tel"));
                user.setEmail(resultSet.getString("email"));
                user.setSex(resultSet.getString("sex"));
                user.setPosition(resultSet.getString("position"));
                user.setRealName(resultSet.getString("realname"));
                user.setUnit(resultSet.getString("unit"));
                user.setState(resultSet.getInt("state"));
                result.add(user);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            return result;
        }
    }
}
